package com.itlong.cloud.controller.robotControl;

import com.itlong.cloud.POJO.DTO.app.AppPadBotRobotDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyPadBotDeliveryTaskDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyDeliveryTaskGetPageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.iottp.IPadBotRobotServiceClient;
import com.itlong.cloud.project.property.IPropertyDeliveryTaskServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      机器人派送任务控制器
 * </desc>
 *
 * @createDate 2021/1/7
 **/
@RestController
@RequestMapping("robotControl/deliveryTask")
public class PropertyDeliveryTaskController extends BaseController {

    @Autowired
    private IPadBotRobotServiceClient iPadBotRobotServiceClient;

    @Autowired
    private IPropertyDeliveryTaskServiceClient iPropertyDeliveryTaskServiceClient;


    /**
     * <desc>
     *      设置回调地址
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/7
     */
    @UnParamEncrypt
    @UnAuthAccessRequired
    @PostMapping("/setCallbackUrl")
    public Object setCallbackUrl(PropertyPadBotDeliveryTaskDTO deliveryTaskDTO){
        try {
            if (deliveryTaskDTO == null || StringUtils.isBlank(deliveryTaskDTO.getSerialNumber())
                    || StringUtils.isBlank(deliveryTaskDTO.getUrlType()) || StringUtils.isBlank(deliveryTaskDTO.getUrl())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPadBotRobotServiceClient.configCallbackUrl(deliveryTaskDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【机器人派送任务setCallbackUrl】设置回调地址失败", e);
        }
    }

    /**
     * <desc>
     *      获取回调地址
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/7
     */
    @UnParamEncrypt
    @UnAuthAccessRequired
    @PostMapping("/getCallbackList")
    public Object getCallbackList(PropertyPadBotDeliveryTaskDTO propertyPadBotDeliveryTaskDTO){
        try {
            String url = iPadBotRobotServiceClient.getCallbackUrl(propertyPadBotDeliveryTaskDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), url);
        }catch (Exception e){
            throw new DataAccessException("【机器人派送任务getCallbackList】获取回调地址列表失败", e);
        }
    }

    /**
     * <desc>
     *      获取派送任务分页列表
     * </desc>
     *
     * @param deliveryTaskDTO 派送任务参数DTO
     * @return 格式化响应 分页列表
     * @Author Zhu.yj
     * @createDate  2021/1/8
     */
    @PostMapping("/getPage")
    public Object getPage(PropertyPadBotDeliveryTaskDTO deliveryTaskDTO, HttpServletRequest request){
        try {
            deliveryTaskDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyPadBotDeliveryTaskDTO.class, request);
            if (deliveryTaskDTO == null || StringUtils.isBlank(deliveryTaskDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyDeliveryTaskGetPageVO> getPageVOList  = iPropertyDeliveryTaskServiceClient.getPage(deliveryTaskDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), getPageVOList);
        }catch (Exception e){
            throw new DataAccessException("【机器人派送任务getPage】获取分页列表失败", e);

        }
    }

    /**
     * <desc>
     *      取消派送任务
     * </desc>
     *
     * @param appPadBotRobotDTO 派送任务参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/8
     */
    @PostMapping("/cancelDeliveryTask")
    public Object cancelDeliveryTask(AppPadBotRobotDTO appPadBotRobotDTO, HttpServletRequest request){
        try {
            appPadBotRobotDTO = HttpProtocolUtil.parseRequestParamToDTO(AppPadBotRobotDTO.class, request);
            if (appPadBotRobotDTO == null || StringUtils.isBlank(appPadBotRobotDTO.getProjectId())
                    || StringUtils.isBlank(appPadBotRobotDTO.getTaskId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPadBotRobotServiceClient.cancelDeliveryTask(appPadBotRobotDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【机器人派送任务cancelDeliveryTask】取消派送任务失败", e);
        }
    }

    /**
     * <desc>
     *      获取目标点列表
     * </desc>
     *
     * @param deliveryTaskDTO 派送任务参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/8
     */
    @PostMapping("/getTargetList")
    public Object getTargetList(PropertyPadBotDeliveryTaskDTO deliveryTaskDTO, HttpServletRequest request){
        try {
            deliveryTaskDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyPadBotDeliveryTaskDTO.class, request);
            if (deliveryTaskDTO == null || StringUtils.isBlank(deliveryTaskDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<Map<String, Object>> targetPointList = iPropertyDeliveryTaskServiceClient.getTargetPointList(null);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), targetPointList);
        }catch (Exception e){
            throw new DataAccessException("【机器人派送任务cancelDeliveryTask】获取目标点列表失败", e);
        }
    }

}
